package com.android.gufengmall.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.TalentAdapter;
import com.android.gufengmall.bean.TalentBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.TalentBeanDao;

import java.util.ArrayList;
import java.util.List;

/**
 * 琴棋书画才艺知识专区
 */
public class TalentFragment extends Fragment implements TalentAdapter.CommentListener {

    private RecyclerView mRvTalent;

    private ArrayList<TalentBean> mTalentBeans;
    private TalentAdapter mTalentAdapter;
    private App mMyApp;
    private DaoSession mDaoSession;
    private TalentBeanDao mTalentBeanDao;

    public TalentFragment() {
    }

    public static TalentFragment newInstance() {
        return new TalentFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_talent, container, false);
        initView(view);
        initData();
        return view;
    }


    private void initView(View view) {
        mRvTalent = view.findViewById(R.id.rv_talent);
    }

    private void initData() {
        mTalentBeans = new ArrayList<>();

        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mTalentBeanDao = mDaoSession.getTalentBeanDao();
        List<TalentBean> talentBeans = mTalentBeanDao.loadAll();
        mTalentBeans.addAll(talentBeans);

        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvTalent.setLayoutManager(layoutManager);

        mTalentAdapter = new TalentAdapter(requireContext(), mTalentBeans);
        mTalentAdapter.setOnCommentListener(this);
        mRvTalent.setAdapter(mTalentAdapter);

    }

    @Override
    public void onLikeListener(int position) {
        TalentBean talentBean = mTalentBeanDao.loadByRowId(position);
        boolean isLike = talentBean.getIsLike();
        talentBean.setIsLike(!isLike);
        mTalentBeanDao.update(talentBean);
        if (isLike) {
            Toast.makeText(requireContext(), "取消点赞", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "点赞成功", Toast.LENGTH_SHORT).show();
        }
        mTalentAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCollectListener(int position) {
        TalentBean talentBean = mTalentBeanDao.loadByRowId(position);
        boolean isCollect = talentBean.getIsCollect();
        talentBean.setIsCollect(!isCollect);
        mTalentBeanDao.update(talentBean);
        if (isCollect) {
            Toast.makeText(requireContext(), "取消点赞", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "点赞成功", Toast.LENGTH_SHORT).show();
        }
        mTalentAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemSelectListener(String position) {
        Intent intent = new Intent(requireContext(), CommentActivity.class);
        intent.putExtra("status", 3);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}